<?php
namespace app\admin\model;
/* *
 * 自定义单页功能
 * */
class nav extends \app\base\model\BaseModel{
    protected $table ='admin_nav';  //模型表

    //列表
    public function _select($condition = array()){
        return $this->where($condition)->order('neworder asc,id asc')->select();
    }

    //列表
    public function top_select($condition = array()){
        $rel = $this->where($condition)->order('neworder asc,id asc')->select();
        foreach ($rel as $key => $value) {
            $menu = model('nav')->_select_lists($value['id']);
            if(!empty($menu['0']['list'])){
                $nav[$key] = $value;
            }
        }
        return $nav;
    }

    //单个
    public function _find($condition){
        return $this->where($condition)->find();
    }

    //删除
    public function _delete($fid){
        //查看是否有子分类
        $subclass = $this->where(array('pid'=>$fid))->find(); 
        if($subclass){
            return '请先删除下级菜单';
        }

        $result = $this->where(array('id'=>$fid))->delete();
        if($result){
            return 0;
        }else{
            return '删除失败';
        }
    }  

    //编辑
    public function _update($data,$condition = NULL){
        if($condition){
            return $this->where($condition)->data($data)->update();
        }else{
            return $this->data($data)->insert();
        }
    }  

    //菜单
    public function _select_lists($id){
        $groupid = intval($_SESSION['admin_userInfo']['groupid']);
        $userid = intval($_SESSION['admin_userInfo']['id']);
        $auth_group = model('auth')->_find(array('id'=>$groupid));  //权限组
        if(!$auth_group['app_nav'])return false;
        $auth = json_decode($auth_group['app_nav'],TRUE); 
        $rel = $this->where(array('pid'=>$id))->order('neworder asc,id asc')->select();      
        if($rel){
            foreach ($rel as $key => $value) {
                $nav[$key]['title'] = htmlspecialchars_decode($value['name']);
                $menu = $this->where(array('pid'=>$value['id']))->order('neworder asc,id asc')->select();
                if($menu){
                    foreach ($menu as $k => $v){
                        if($auth[0] >= 1){
                            if(in_array($v['id'],$auth)){
                                $nav[$key]['list'][htmlspecialchars_decode($v['name'])] = url($v['url']);
                            }
                        }else{
                            $nav[$key]['list'][htmlspecialchars_decode($v['name'])] = url($v['url']);
                        }
                        
                    }
                }
            }
        }
        return $nav;
    }


    //权限管理
    public function nav_lists($fid = 0){
        $rel = $this->where(array('pid'=>$fid))->field('id,name')->order('neworder asc,id asc')->select();
        if($rel){
            foreach($rel as  $key => $rs){
                $rel[$key]['menu'] = $this->nav_lists($rs['id']);
            }
        }
        return $rel;
    }

    //当前路径
    public function path($pid = 0,$url = null) {
        $condition['id'] = intval($pid);
         $result = $this->where($condition)->field('id,pid,name')->find();
        if ($result) {
            $Str = $this->path($result['pid'],$url);
            if($url){
                $Str .= ' / <a href="' . url(''.$url.'',array(id=>$result['id'])).'">' . htmlspecialchars_decode($result['name']) . '</a>';
            }else{
                $Str .= ' / <a href="###">' . htmlspecialchars_decode($result['name']) . '</a>';
            }
        }
        return $Str;
    }

    //统计层级
    public function count_path($pid = 0) {
        $condition['id'] = intval($pid);
        $result = $this->where($condition)->field('id,pid,name')->find();
        if ($result) {
            $Str = $this->count_path($result['pid']);
            $Str[] = $result['id'];
        }
        return $Str;
    }

}